const test = require('ava')
const path = require('path')

const readPolyDataLocalFileSync = require(path.resolve(__dirname, '..', 'dist', 'readPolyDataLocalFileSync.js'))

test('readPolyDataLocalFileSync reads a vtkPolyData in the legacy VTK file format', (t) => {
  const verifyPolyData = (polyData) => {
    t.is(polyData.vtkClass, 'vtkPolyData')
    t.is(polyData.points.vtkClass, 'vtkPoints')
    t.is(polyData.points.name, 'points')
    t.is(polyData.points.numberOfComponents, 3)
    t.is(polyData.points.dataType, 'Float32Array')
    t.is(polyData.points.size, 8709)
    t.is(polyData.points.buffer.byteLength, 34836)
    t.is(polyData.polys.vtkClass, 'vtkCellArray')
    t.is(polyData.polys.name, 'polys')
    t.is(polyData.polys.numberOfComponents, 1)
    t.is(polyData.polys.dataType, 'Int32Array')
    t.is(polyData.polys.size, 15593)
    t.is(polyData.polys.buffer.byteLength, 62372)
  }

  const testPolyDataFilePath = path.resolve(__dirname, '..', 'build', 'ExternalData', 'test', 'Input', 'cow.vtk')
  const polyData = readPolyDataLocalFileSync(testPolyDataFilePath)
  verifyPolyData(polyData)
})

test('readPolyDataLocalFileSync reads a vtkUnstructuredGrid in the legacy VTK file format', (t) => {
  const verifyPolyData = (polyData) => {
    t.is(polyData.vtkClass, 'vtkPolyData')
    t.is(polyData.points.vtkClass, 'vtkPoints')
    t.is(polyData.points.name, 'points')
    t.is(polyData.points.numberOfComponents, 3)
    t.is(polyData.points.dataType, 'Float32Array')
    t.is(polyData.points.size, 81)
    t.is(polyData.verts.vtkClass, 'vtkCellArray')
    t.is(polyData.verts.name, 'verts')
    t.is(polyData.verts.numberOfComponents, 1)
    t.is(polyData.verts.dataType, 'Int32Array')
    t.is(polyData.verts.size, 2)
    t.is(polyData.lines.vtkClass, 'vtkCellArray')
    t.is(polyData.lines.name, 'lines')
    t.is(polyData.lines.numberOfComponents, 1)
    t.is(polyData.lines.dataType, 'Int32Array')
    t.is(polyData.lines.size, 6)
    t.is(polyData.strips.vtkClass, 'vtkCellArray')
    t.is(polyData.strips.name, 'strips')
    t.is(polyData.strips.numberOfComponents, 1)
    t.is(polyData.strips.dataType, 'Int32Array')
    t.is(polyData.strips.size, 7)
    t.is(polyData.polys.vtkClass, 'vtkCellArray')
    t.is(polyData.polys.name, 'polys')
    t.is(polyData.polys.numberOfComponents, 1)
    t.is(polyData.polys.dataType, 'Int32Array')
    t.is(polyData.polys.size, 94)
    t.is(polyData.pointData.vtkClass, 'vtkDataSetAttributes')
    t.is(polyData.pointData.arrays[0].data.name, 'scalars')
    t.is(polyData.pointData.arrays[0].data.numberOfComponents, 1)
    t.is(polyData.pointData.arrays[0].data.dataType, 'Float32Array')
    t.is(polyData.pointData.arrays[0].data.size, 27)
    t.is(polyData.pointData.arrays[1].data.name, 'vectors')
    t.is(polyData.pointData.arrays[1].data.numberOfComponents, 3)
    t.is(polyData.pointData.arrays[1].data.dataType, 'Float32Array')
    t.is(polyData.pointData.arrays[1].data.size, 81)
  }

  const testPolyDataFilePath = path.resolve(__dirname, '..', 'build', 'ExternalData', 'test', 'Input', 'uGridEx.vtk')
  const polyData = readPolyDataLocalFileSync(testPolyDataFilePath)
  verifyPolyData(polyData)
})

test('readPolyDataLocalFileSync reads a vtkPolyData in the VTK XML file format', (t) => {
  const verifyPolyData = (polyData) => {
    t.is(polyData.vtkClass, 'vtkPolyData')
    t.is(polyData.points.vtkClass, 'vtkPoints')
    t.is(polyData.points.name, 'points')
    t.is(polyData.points.numberOfComponents, 3)
    t.is(polyData.points.dataType, 'Float32Array')
    t.is(polyData.points.size, 6183)
    t.is(polyData.polys.vtkClass, 'vtkCellArray')
    t.is(polyData.polys.name, 'polys')
    t.is(polyData.polys.numberOfComponents, 1)
    t.is(polyData.polys.dataType, 'Int32Array')
    t.is(polyData.polys.size, 11759)
    t.is(polyData.pointData.vtkClass, 'vtkDataSetAttributes')
    t.is(polyData.pointData.arrays[0].data.name, 'Ids')
    t.is(polyData.pointData.arrays[0].data.numberOfComponents, 1)
    t.is(polyData.pointData.arrays[0].data.dataType, 'Int32Array')
    t.is(polyData.pointData.arrays[0].data.size, 2061)
    t.is(polyData.pointData.arrays[1].data.name, 'AsH3')
    t.is(polyData.pointData.arrays[1].data.numberOfComponents, 1)
    t.is(polyData.pointData.arrays[1].data.dataType, 'Float64Array')
    t.is(polyData.pointData.arrays[1].data.size, 2061)
    t.is(polyData.cellData.arrays[1].data.name, 'ObjectId')
    t.is(polyData.cellData.arrays[1].data.numberOfComponents, 1)
    t.is(polyData.cellData.arrays[1].data.dataType, 'Int32Array')
    t.is(polyData.cellData.arrays[1].data.size, 2547)
  }

  const testPolyDataFilePath = path.resolve(__dirname, '..', 'build', 'ExternalData', 'test', 'Input', 'diskout.vtp')
  const polyData = readPolyDataLocalFileSync(testPolyDataFilePath)
  verifyPolyData(polyData)
})

test('readPolyDataLocalFileSync reads a vtkUnstructuredGrid in the VTK XML file format', (t) => {
  const verifyPolyData = (polyData) => {
    t.is(polyData.vtkClass, 'vtkPolyData')
    t.is(polyData.points.vtkClass, 'vtkPoints')
    t.is(polyData.points.name, 'points')
    t.is(polyData.points.numberOfComponents, 3)
    t.is(polyData.points.dataType, 'Float32Array')
    t.is(polyData.points.size, 336)
    t.is(polyData.verts.vtkClass, 'vtkCellArray')
    t.is(polyData.verts.name, 'verts')
    t.is(polyData.verts.numberOfComponents, 1)
    t.is(polyData.verts.dataType, 'Int32Array')
    t.is(polyData.verts.size, 2)
    t.is(polyData.lines.vtkClass, 'vtkCellArray')
    t.is(polyData.lines.name, 'lines')
    t.is(polyData.lines.numberOfComponents, 1)
    t.is(polyData.lines.dataType, 'Int32Array')
    t.is(polyData.lines.size, 6)
    t.is(polyData.polys.vtkClass, 'vtkCellArray')
    t.is(polyData.polys.name, 'polys')
    t.is(polyData.polys.numberOfComponents, 1)
    t.is(polyData.polys.dataType, 'Int32Array')
    t.is(polyData.polys.size, 203)
    t.is(polyData.pointData.vtkClass, 'vtkDataSetAttributes')
    t.is(polyData.pointData.arrays[0].data.name, 'pointScalars')
    t.is(polyData.pointData.arrays[0].data.numberOfComponents, 1)
    t.is(polyData.pointData.arrays[0].data.dataType, 'Float32Array')
    t.is(polyData.pointData.arrays[0].data.size, 112)
    t.is(polyData.pointData.arrays[1].data.name, 'pointVectors')
    t.is(polyData.pointData.arrays[1].data.numberOfComponents, 3)
    t.is(polyData.pointData.arrays[1].data.dataType, 'Float32Array')
    t.is(polyData.pointData.arrays[1].data.size, 336)
  }

  const testPolyDataFilePath = path.resolve(__dirname, '..', 'build', 'ExternalData', 'test', 'Input', 'elements.vtu')
  const polyData = readPolyDataLocalFileSync(testPolyDataFilePath)
  verifyPolyData(polyData)
})

test('readPolyDataLocalFileSync reads a vtkRectilinearGrid in the VTK XML file format', (t) => {
  const verifyPolyData = (polyData) => {
    t.is(polyData.vtkClass, 'vtkPolyData')
    t.is(polyData.points.vtkClass, 'vtkPoints')
    t.is(polyData.points.name, 'points')
    t.is(polyData.points.numberOfComponents, 3)
    t.is(polyData.points.dataType, 'Float32Array')
    t.is(polyData.points.size, 13518)
    t.is(polyData.polys.vtkClass, 'vtkCellArray')
    t.is(polyData.polys.name, 'polys')
    t.is(polyData.polys.numberOfComponents, 1)
    t.is(polyData.polys.dataType, 'Int32Array')
    t.is(polyData.polys.size, 22520)
    t.is(polyData.pointData.vtkClass, 'vtkDataSetAttributes')
    t.is(polyData.pointData.arrays[0].data.name, 'scalars')
    t.is(polyData.pointData.arrays[0].data.numberOfComponents, 1)
    t.is(polyData.pointData.arrays[0].data.dataType, 'Float32Array')
    t.is(polyData.pointData.arrays[0].data.size, 4506)
    t.is(polyData.pointData.arrays[1].data.name, 'vectors')
    t.is(polyData.pointData.arrays[1].data.numberOfComponents, 3)
    t.is(polyData.pointData.arrays[1].data.dataType, 'Float32Array')
    t.is(polyData.pointData.arrays[1].data.size, 13518)
  }

  const testPolyDataFilePath = path.resolve(__dirname, '..', 'build', 'ExternalData', 'test', 'Input', 'RectGrid2.vtr')
  const polyData = readPolyDataLocalFileSync(testPolyDataFilePath)
  verifyPolyData(polyData)
})

test('readPolyDataLocalFileSync reads an Exodus file', (t) => {
  const verifyPolyData = (polyData) => {
    t.is(polyData.vtkClass, 'vtkPolyData')
    t.is(polyData.points.vtkClass, 'vtkPoints')
    t.is(polyData.points.name, 'points')
    t.is(polyData.points.numberOfComponents, 3)
    t.is(polyData.points.dataType, 'Float32Array')
    t.is(polyData.points.size, 25497)
    t.is(polyData.polys.vtkClass, 'vtkCellArray')
    t.is(polyData.polys.name, 'polys')
    t.is(polyData.polys.numberOfComponents, 1)
    t.is(polyData.polys.dataType, 'Int32Array')
    t.is(polyData.polys.size, 9940)
    t.is(polyData.cellData.vtkClass, 'vtkDataSetAttributes')
    t.is(polyData.cellData.arrays[0].data.name, 'ObjectId')
    t.is(polyData.cellData.arrays[0].data.numberOfComponents, 1)
    t.is(polyData.cellData.arrays[0].data.dataType, 'Int32Array')
    t.is(polyData.cellData.arrays[0].data.size, 1988)
  }

  const testPolyDataFilePath = path.resolve(__dirname, '..', 'build', 'ExternalData', 'test', 'Input', 'disk_out_ref.ex2')
  const polyData = readPolyDataLocalFileSync(testPolyDataFilePath)
  verifyPolyData(polyData)
})

test('readPolyDataLocalFileSync reads a PLY file', (t) => {
  const verifyPolyData = (polyData) => {
    t.is(polyData.vtkClass, 'vtkPolyData')
    t.is(polyData.points.vtkClass, 'vtkPoints')
    t.is(polyData.points.name, 'points')
    t.is(polyData.points.numberOfComponents, 3)
    t.is(polyData.points.dataType, 'Float32Array')
    t.is(polyData.points.size, 107841)
    t.is(polyData.polys.vtkClass, 'vtkCellArray')
    t.is(polyData.polys.name, 'polys')
    t.is(polyData.polys.numberOfComponents, 1)
    t.is(polyData.polys.dataType, 'Int32Array')
    t.is(polyData.polys.size, 277804)
  }

  const testPolyDataFilePath = path.resolve(__dirname, '..', 'build', 'ExternalData', 'test', 'Input', 'bunny.ply')
  const polyData = readPolyDataLocalFileSync(testPolyDataFilePath)
  verifyPolyData(polyData)
})
